package com.eastone.user.serverapi.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eastone.user.serverapi.dao.InfoRoleAuthDao;
import com.eastone.user.serverapi.model.InfoRoleAuth;
import com.eastone.user.serverapi.service.InfoRoleAuthService;
import com.yjx.framework.cache.ServiceResultCodeCache;
import com.yjx.framework.model.ServiceResult;

@Service("infoRoleAuthService")
public class InfoRoleAuthServiceImpl implements InfoRoleAuthService {
	@Autowired
	private InfoRoleAuthDao infoRoleAuthDao;
	private static ServiceResultCodeCache resultCode = ServiceResultCodeCache.getInstance();

	@Override
	public InfoRoleAuth get(int id) {
		return infoRoleAuthDao.get(id);
	}

	@Override
	public ServiceResult<String> add(InfoRoleAuth infoRoleAuth) {
		ServiceResult<String> serviceResult = new ServiceResult<String>();
		int result = infoRoleAuthDao.add(infoRoleAuth);
		if (result == 1) {
			serviceResult.setCode(resultCode.get("common.sucess"));
		} else {
			serviceResult.setCode(resultCode.get("common.fail"));
		}
		return serviceResult;
	}

	@Override
	public ServiceResult<String> addSelective(InfoRoleAuth infoRoleAuth) {
		ServiceResult<String> serviceResult = new ServiceResult<String>();
		int result = infoRoleAuthDao.addSelective(infoRoleAuth);
		if (result == 1) {
			serviceResult.setCode(resultCode.get("common.sucess"));
		} else {
			serviceResult.setCode(resultCode.get("common.fail"));
		}
		return serviceResult;
	}

	@Override
	public ServiceResult<String> update(InfoRoleAuth infoRoleAuth) {
		ServiceResult<String> serviceResult = new ServiceResult<String>();
		int result = infoRoleAuthDao.update(infoRoleAuth);
		if (result == 1) {
			serviceResult.setCode(resultCode.get("common.sucess"));
		} else {
			serviceResult.setCode(resultCode.get("common.fail"));
		}
		return serviceResult;
	}

	@Override
	public ServiceResult<String> updateSelective(InfoRoleAuth infoRoleAuth) {
		ServiceResult<String> serviceResult = new ServiceResult<String>();
		int result = infoRoleAuthDao.updateSelective(infoRoleAuth);
		if (result == 1) {
			serviceResult.setCode(resultCode.get("common.sucess"));
		} else {
			serviceResult.setCode(resultCode.get("common.fail"));
		}
		return serviceResult;
	}

	@Override
	public ServiceResult<String> delete(InfoRoleAuth infoRoleAuth) {
		ServiceResult<String> serviceResult = new ServiceResult<String>();
		int result = infoRoleAuthDao.delete(infoRoleAuth);
		if (result == 1) {
			serviceResult.setCode(resultCode.get("common.sucess"));
		} else {
			serviceResult.setCode(resultCode.get("common.fail"));
		}
		return serviceResult;
	}

	@Override
	public List<InfoRoleAuth> getList() {
		return infoRoleAuthDao.getList();
	}

	@Override
	public int getCount() {
		return infoRoleAuthDao.getCount();
	}

	@Override
	public List<InfoRoleAuth> getByRoleId(String roleId) {
		return infoRoleAuthDao.getByRoleId(roleId);
	}

	@Override
	public ServiceResult<String> saveOrUpdate(InfoRoleAuth infoRoleAuth) {
		ServiceResult<String> serviceResult = new ServiceResult<String>();
		int result = 0;
		InfoRoleAuth infoRoleAuthT = infoRoleAuthDao.getByRoleAndAuthId(infoRoleAuth);
		if(infoRoleAuthT==null){
			result = infoRoleAuthDao.add(infoRoleAuth);
		}else{
			infoRoleAuth.setId(infoRoleAuthT.getId());
			result = infoRoleAuthDao.update(infoRoleAuth);
		}
		if (result == 1) {
			serviceResult.setCode(resultCode.get("common.sucess"));
		} else {
			serviceResult.setCode(resultCode.get("common.fail"));
		}
		return serviceResult;	
	}
	

}
